import pandas as pd  
import jieba  
import re  
import openpyxl  
from snownlp import SnowNLP
  
# 定义一个函数来检查并保留中文字符  
def is_chinese_regex(word):  
    return re.match(r'[\u4e00-\u9fa5]+', word) is not None  
  
# 读取Excel文件  
file_path = 'd:\\chapter9_hemaApp_comment.xlsx'  # 替换为你的Excel文件路径  
df = pd.read_excel(file_path, usecols=['content'])  # 假设 content列为评价内容
sentimentslist = []
for index,row in df.iterrows():
    text = row['content']
    s = SnowNLP(text)
    sentimentslist.append(s.sentiments)
#print(sentimentslist)

pos=0
neg=0
for j in sentimentslist:
    if j >=0.5:
        pos +=1
    else:
        neg +=1
print(f"积极的评论占比：{round(pos/len(df.content)*100,3)}%")
print(f"消极的评论占比：{round(neg/len(df.content)*100,3)}%")